package com.example.compose.timer

import androidx.compose.animation.animateColor
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.tween
import androidx.compose.animation.core.updateTransition
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color

class CircularTransitionData(progress: State<Float>, color: State<Color>) {
    val progress by progress
    val color by color
}

@Composable
fun updateCircularTransitionData(remainingTime: Long, totalTime: Long): CircularTransitionData {
    val transition = updateTransition(targetState = remainingTime, label = null)
    val progress = transition.animateFloat(
        transitionSpec = {
            tween(delayMillis = 800, easing = LinearEasing)
        },
        label = ""
    ) { remTime ->
        if (remTime < 0) {
            360f
        } else {
            360f -((360f/ totalTime) * (totalTime - remTime))
        }
    }

    val color = transition.animateColor(
        transitionSpec = {
            tween(delayMillis = 800, easing = LinearEasing)
        },
        label = ""
    ) {
        if (it < 180f && it > 90f) {
            Color.Yellow
        } else if (it <= 90f) {
            Color.Red
        } else {
            Color.Green
        }
    }

    return remember(key1 = transition) {
        CircularTransitionData(progress, color)
    }
}